import { message, Form } from 'antd'
import { useState } from 'react'
import { Userspassword } from '@/apis/adminuser'
import { clearUserInfo } from '@/store/modules/user'
import { useNavigate } from 'react-router-dom'
import { useDispatch } from 'react-redux'
function useAdminPasswd() {
    //修改数据
    const [pasloading, setPasLoading] = useState(false)
    const [pasopen, setPasOpen] = useState(false)
    const [form] = Form.useForm()
    const navigate = useNavigate()
    //触发用户个人信息 action
    const dispatch = useDispatch()
    //显示弹窗
    const showPasModal = async (pasid) => {
        setPasOpen(true)
        form.setFieldsValue({ pasid: pasid, oldpassword: '', newpassword: '' })
    };
    //关闭弹窗
    const handlePasCancel = () => {
        setPasOpen(false)
    }
    //提交数据
    const handlePasOk = async (val) => {
        setPasLoading(true)
        // console.log(val)
        const { oldpassword, newpassword, verifypassword } = val
        const data = { oldpassword, newpassword }
        if (verifypassword != newpassword) {
            message.error("新密码与确认密码不一致")
            setPasLoading(false)
            return
        }
        //修改密码
        const res = await Userspassword(data)
        if (res.code == 200) {
            // setPasOpen(false)
            message.success('修改成功')
            dispatch(clearUserInfo())
            navigate("/login")
        }
        else {
            message.error(res.message)
        }
        setPasLoading(false)
    }

    return { pasloading, pasopen, form, showPasModal, handlePasCancel, handlePasOk }
}


export default useAdminPasswd
